package com.ywu.application.standard.fileSys.service;

import com.ywu.boot.context.YwuContext;
import com.ywu.boot.context.YwuContextHolder;
import com.ywu.common.entity.PageInfo;
import com.ywu.common.result.CommonResult;
import com.ywu.common.utils.TimerIdGenerateUtil;
import com.ywu.application.standard.fileSys.config.YwuFileSysAppConfig;
import com.ywu.application.standard.fileSys.data.YwuFIleTaskRepository;
import com.ywu.application.standard.fileSys.data.YwuFileSysRepository;
import com.ywu.application.standard.fileSys.dto.YwuFIleTaskDto;
import com.ywu.application.standard.fileSys.entity.YwuFIleTaskEntity;
import com.ywu.application.standard.fileSys.entity.YwuFileSysEntity;
import com.ywu.application.standard.fileSys.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName YwuFIleTaskService
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/9/15 12:15
 * @Version 1.0
 **/
@Service
public class YwuFIleTaskService {
    @Autowired
    private YwuFIleTaskRepository repository;

    @Autowired
    private YwuFileSysRepository fileSysRepository;

    @Autowired
    private YwuFileSysAppConfig ywuFileSysAppConfig;

    @Autowired
    private FileUtils fileUtils;

    /**
    * @Author GroundDemo
    * @Description 添加任务
    * @Date 15:31 2024/9/15
    * @Param [ywuFIleTaskEntity]
    * @return void
    **/
    public void insertTask(YwuFIleTaskEntity ywuFIleTaskEntity) {
        // 插入数据
        repository.addTask(ywuFIleTaskEntity);
    }

    /**
    * @Author GroundDemo
    * @Description 添加任务
    * @Date 15:41 2024/9/15
    * @Param [fileId, taskOperateTypeUpload]
    * @return java.lang.String
    **/
    public String insertTask(String fileId, String taskOperateTypeUpload) {
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        String taskId = TimerIdGenerateUtil.nextId();
        YwuFIleTaskEntity ywuFIleTaskEntity = new YwuFIleTaskEntity();
        ywuFIleTaskEntity.setFileId(fileId);
        ywuFIleTaskEntity.setTaskState(YwuFIleTaskEntity.TASK_STATE_RUNNING);
        ywuFIleTaskEntity.setOperateType(taskOperateTypeUpload);
        ywuFIleTaskEntity.setId(taskId);
        ywuFIleTaskEntity.setOperateBy(currentContext.getYwuContextUser().getUserAccount());
        ywuFIleTaskEntity.createInfo(currentContext.getYwuContextUser().getUserAccount());
        insertTask(ywuFIleTaskEntity);
        return taskId;
    }

    /**
    * @Author GroundDemo
    * @Description 更新任务
    * @Date 15:32 2024/9/15
    * @Param [updateEntity]
    * @return void
    **/
    public void updateTask(YwuFIleTaskEntity updateEntity) {
        // 更新数据
        repository.updateTask(updateEntity);
    }

    /**
    * @Author GroundDemo
    * @Description 更新任务
    * @Date 15:43 2024/9/15
    * @Param [taskId, message, taskState]
    * @return void
    **/
    public void updateTask(String taskId, String message, String taskState) {
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        YwuFIleTaskEntity updateEntity = new YwuFIleTaskEntity();
        updateEntity.setId(taskId);
        updateEntity.setOperateMessage(message);
        updateEntity.setTaskState(taskState);
        updateEntity.updateInfo(currentContext.getYwuContextUser().getUserAccount());
        updateTask(updateEntity);
    }

    /**
    * @Author GroundDemo
    * @Description 查询任务列表
    * @Date 16:06 2024/9/15
    * @Param [taskDto]
    * @return com.ywu.common.result.CommonResult<com.ywu.file.sys.standard.dto.YwuFIleTaskDto>
    **/
    public CommonResult<List<YwuFIleTaskDto>> queryTaskList(YwuFIleTaskDto taskDto) {
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        // 查询当前用户的任务数据，默认分页数50
        PageInfo pageInfo = taskDto.getPageInfo();
        pageInfo.setPageSize(50);
        taskDto.setOperateBy(currentContext.getYwuContextUser().getUserAccount());
        List<YwuFIleTaskDto> dtoList = repository.queryTaskList(taskDto);
        if (CollectionUtils.isEmpty(dtoList)) {
            return CommonResult.success(new ArrayList<>());
        }
        // 计算文件传输百分比
        for (YwuFIleTaskDto dto : dtoList) {
            Integer partTotal = dto.getPartTotal();
            Integer partNum = dto.getPartNum();
            if (partTotal == 0) {
                dto.setPercentage(100);
            } else {
                double percentDouble = ((double) partNum / (double) partTotal) * 100.0;
                dto.setPercentage((int) percentDouble);
            }
        }
        return CommonResult.success(dtoList);
    }

    /**
    * @Author GroundDemo
    * @Description 创建任务记录
    * @Date 10:38 2024/9/16
    * @Param [taskDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @Transactional
    public CommonResult<Object> taskCreate(YwuFIleTaskDto taskDto) {
        // 校验文件后缀类型
        String fileType = fileUtils.getFileType(taskDto.getFileName());
        String fileId = TimerIdGenerateUtil.nextId();
        // 添加一条任务记录
        String taskId = insertTask(fileId, YwuFIleTaskEntity.TASK_OPERATE_TYPE_UPLOAD);
        // 添加一条文件记录，状态为 PARTING
        YwuFileSysEntity ywuFileSysEntity = new YwuFileSysEntity();
        ywuFileSysEntity.setId(fileId);
        ywuFileSysEntity.setFileId(ywuFileSysAppConfig.getFileIdPrefix() + ywuFileSysEntity.getId());
        String appId = YwuContextHolder.getCurrentContext().getYwuContextApp().getAppId();
        ywuFileSysEntity.setFileAppOwner(appId);
        ywuFileSysEntity.setIsShare(false);
        ywuFileSysEntity.setPartTotal(taskDto.getPartTotal());
        ywuFileSysEntity.setPartNum(0);
        ywuFileSysEntity.setFileName(taskDto.getFileName());
        ywuFileSysEntity.setFileStatus(YwuFileSysEntity.FILE_STATUS_PARTING);
        ywuFileSysEntity.createInfo(YwuContextHolder.getCurrentContext().getYwuContextUser().getUserAccount());
        fileSysRepository.addFileInfo(ywuFileSysEntity);
        YwuFIleTaskDto res = new YwuFIleTaskDto();
        res.setId(taskId);
        return CommonResult.success(res);
    }

    /**
    * @Author GroundDemo
    * @Description 根据ID查询任务信息
    * @Date 11:05 2024/9/16
    * @Param [search]
    * @return com.ywu.file.sys.standard.entity.YwuFIleTaskEntity
    **/
    public YwuFIleTaskEntity getEntityById(YwuFIleTaskEntity search) {
        return repository.getEntityById(search);
    }
}
